﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using NhaThuocOnline.Entities;
using NhaThuocOnline.Repositories;
using NhaThuocOnline.ViewModels;

namespace NhaThuocOnline.Service
{
    public class NhaThuocService
    {
        private void InsertOrUpdate(ViewModelsNhaThuoc nt)
        {
            var en = Mapper.Map<ViewModelsNhaThuoc, NhaThuoc>(nt);
            using (NhaThuocRepository repo = new NhaThuocRepository())
            {
                repo.InsertOrUpdate(en);
                repo.Save();
            }
        }
        private void Delete(int id)
        {
            using (NhaThuocRepository repo = new NhaThuocRepository())
            {
                repo.Delete(id);
                repo.Save();
            }
        }
        private List<ViewModelsNhaThuoc> GetAll()
        {
            using (NhaThuocRepository repo = new NhaThuocRepository())
            {
                return Mapper.Map<List<NhaThuoc>, List<ViewModelsNhaThuoc>>(repo.GetAll.ToList());
            }
        }
        private ViewModelsNhaThuoc GetNhaThuocById(int id)
        {
            using (NhaThuocRepository repo = new NhaThuocRepository())
            {
                return Mapper.Map<NhaThuoc, ViewModelsNhaThuoc>(repo.GetAll.Where(bn => bn.Id == id).FirstOrDefault());
            }
        }
        //Insert ViewModelNhaThuoc into NhaThuoc table
        public void ThemMoiNhaThuoc(ViewModelsNhaThuoc vmNhaThuoc)
        {
            try
            {
                InsertOrUpdate(vmNhaThuoc);
            }
            catch (Exception)
            {          
                  
            }
            
        }
    }
}
